package com.cjh.wechat.core.controller;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.cjh.wechat.commom.pojo.Friend;
import com.cjh.wechat.commom.pojo.User;
import com.cjh.wechat.commom.vo.PageVo;
import com.cjh.wechat.commom.vo.SearchFriendResultVo;
import com.cjh.wechat.commom.vo.ShowFriendVo;
import com.cjh.wechat.commom.vo.UserSearchCondition;
import com.cjh.wechat.core.service.FriendService;
import com.cjh.wechat.core.service.UserService;

@Controller
@RequestMapping("/user")
public class UserController
{
    @Resource
    private UserService      userService;
    @Resource
    private FriendService    friendService;
    private static final int PAGE_SIZE = 10;
    
    @ResponseBody
    @RequestMapping(value = "/search", method = RequestMethod.POST)
    public PageVo search(@ModelAttribute UserSearchCondition condition, int page, String id)
    {
        // 获取好友聊天记录
        int total = userService.getSearchNum(condition);
        List<User> users = userService.search(condition, page, PAGE_SIZE);
        List<Friend> friends = friendService.findByUid(id);
        List<SearchFriendResultVo> resultVos = buildResultVo(id, users, friends);
        return buildPageVo(total, resultVos);
    }
    
    @ResponseBody
    @RequestMapping(value = "/showFriend", method = RequestMethod.POST)
    public List<ShowFriendVo> showFriend(String id)
    {
        List<ShowFriendVo> showFriendVos = friendService.showFriend(id);
        return showFriendVos;
    }
    
    @ResponseBody
    @RequestMapping(value = "/modtify", method = RequestMethod.POST)
    public String modtify(@ModelAttribute User user)
    {
        userService.updateByPrimaryKeySelective(user);
        return "success";
    }
    
    private PageVo buildPageVo(int total, List<SearchFriendResultVo> resultVos)
    {
        PageVo pageVo = new PageVo();
        if (total == 0)
        {
            pageVo.setPages(1);
        }
        else
        {
            pageVo.setPages(total % PAGE_SIZE == 0 ? total / PAGE_SIZE : total / PAGE_SIZE + 1);
        }
        pageVo.setData(resultVos);
        return pageVo;
    }
    
    private List<SearchFriendResultVo> buildResultVo(String id, List<User> users, List<Friend> friends)
    {
        List<SearchFriendResultVo> resultVos = new ArrayList<SearchFriendResultVo>();
        for (User user : users)
        {
            boolean isFriend = false;
            if (user.getId().equals(id))
            {
                isFriend = true;
            }
            for (Friend friend : friends)
            {
                if (user.getId().equals(friend.getFid()))
                {
                    isFriend = true;
                }
            }
            resultVos.add(buildSearchFriendResultVo(user, isFriend));
        }
        return resultVos;
    }
    
    private SearchFriendResultVo buildSearchFriendResultVo(User user, boolean isFriend)
    {
        SearchFriendResultVo resultVo = new SearchFriendResultVo();
        resultVo.setAvatar(user.getAvatar());
        if (isFriend)
        {
            resultVo.setIsFriend(1);
        }
        else
        {
            resultVo.setIsFriend(0);
        }
        resultVo.setId(user.getId());
        resultVo.setNo(user.getNo());
        resultVo.setSign(user.getSign());
        resultVo.setStatus(user.getStatus());
        resultVo.setUsername(user.getUsername());
        return resultVo;
    }
}
